package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.domain.Rewards;
import com.bike.bikeserver.domain.RewardsDetail;
import com.bike.bikeserver.domain.vo.RewardsListVo;
import com.bike.bikeserver.mapper.RewardsDetailMapper;
import com.bike.bikeserver.mapper.RewardsMapper;
import com.bike.bikeserver.service.IRewardsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.bike.bikeserver.constant.Constants.REWARDS_GOT;
import static com.bike.bikeserver.constant.Constants.REWARDS_UNGOT;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/7/9 16:44
 */
@Service
public class RewardsDetailServiceImpl implements IRewardsDetailService
{
    @Autowired
    RewardsDetailMapper rewardsDetailMapper;
    @Autowired
    RewardsMapper rewardsMapper;

    @Override
    public CommonResult getRewardsListByNickName(String nickName)
    {
        LambdaQueryWrapper<RewardsDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RewardsDetail::getNickName, nickName);
        List<RewardsDetail> rewardsDetails = rewardsDetailMapper.selectList(wrapper);
        if (rewardsDetails.size() > 0)
        {
            List<RewardsListVo> rewardsListVos = new ArrayList<>();
            // 获取其他信息
            for (RewardsDetail rewardsDetail : rewardsDetails)
            {
                // 设置已有信息
                RewardsListVo rewardsListVo = new RewardsListVo();
                rewardsListVo.setId(rewardsDetail.getId());
                rewardsListVo.setRewardsInfo(rewardsDetail.getRewardsName());
                rewardsListVo.setRewardsDate(rewardsDetail.getCreateTime());
                rewardsListVo.setRewardsState(rewardsDetail.getRewardsGetState());
                if (REWARDS_UNGOT.equals(rewardsDetail.getRewardsGetState()))
                {
                    rewardsListVo.setRewardsImgUrl("/images/icon/rewards-logo-off.png");
                } else
                {
                    rewardsListVo.setRewardsImgUrl("/images/icon/rewards-logo-on.png");
                }
                // 在Rewards表这个查找组装奖券限制信息
                LambdaQueryWrapper<Rewards> wrapperRewards = new LambdaQueryWrapper<>();
                wrapperRewards.eq(Rewards::getRewardsName, rewardsDetail.getRewardsName());
                Rewards rewards = rewardsMapper.selectOne(wrapperRewards);
                rewardsListVo.setRewardsLimit("限" + rewards.getValidLocation() + "|单笔抵" + rewards.getValidPeriod());
                rewardsListVos.add(rewardsListVo);
            }
            return CommonResult.success(rewardsListVos);
        } else
        {
            return CommonResult.error("暂无可用优惠券！");
        }
    }

    @Override
    public CommonResult updateRewardsStateById(Integer id)
    {
        RewardsDetail rewardsDetail = rewardsDetailMapper.selectById(id);
        rewardsDetail.setRewardsGetState(REWARDS_GOT);
        int count = rewardsDetailMapper.updateById(rewardsDetail);
        return CommonResult.success(count);
    }
}